/*******************************************************************************
 * @project: pdf_test
 * @package: com.burns.pdf
 * @file: PdfTest2.java
 * @author: Administrator
 * @created: 2017-6-8
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/

package com.burns.pdf;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

public class PdfTest2 {

	public static void main(String[] args) {

		String source = "C:/CHS-AVIDM4/Design1/Design1.pdf";
		String target = "C:/CHS-AVIDM4/Design1/";
		PdfTest2 pt2 = new PdfTest2();
		pt2.createScreenShoot(source, target);

		int[] rgb = new int[] { 1, 2, 3 };
		System.out.println(rgb[0] + "." + rgb[1] + "." + rgb[2]);
		System.out.println(rgb.toString());

	}

	public boolean createScreenShoot(String source, String target) {
		File file = new File(source);
		if (!file.exists()) {
			System.err.println("路径[" + source + "]对应的pdf文件不存在!");
			return false;
		}
		try {
			RandomAccessFile raf = new RandomAccessFile(file, "r");
			FileChannel channel = raf.getChannel();
			ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
					channel.size());
			PDFFile pdffile = new PDFFile(buf);
			int num = pdffile.getNumPages();
			System.out.println(num);
			for (int i = 0; i < num; i++) {
				PDFPage page = pdffile.getPage(i);

				// get the width and height for the doc at the default zoom
				Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
						.getWidth(), (int) page.getBBox().getHeight());

				// generate the image
				Image img = page.getImage(rect.width, rect.height, // width &
						rect, // clip rect
						null, // null for the ImageObserver
						true, // fill background with white
						true // block until drawing is done
						);

				BufferedImage tag = new BufferedImage(rect.width, rect.height,
						BufferedImage.TYPE_INT_RGB);

				tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height,
						null);
				File f = new File(target + i + ".jpg");
				if (!f.exists()) {
					f.createNewFile();
				}
				FileOutputStream out = new FileOutputStream(target + i + ".jpg");
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
				encoder.encode(tag); // JPEG编码
//				System.out.println(rect.width);
//				System.out.println(rect.height);
//				
//				for (int j = 0; j < rect.width; j++) {
//					for (int j2 = 0; j2 < rect.height; j2++) {
//						int[] rgbArr = getRGB(tag, j, j2);
//						System.out
//						.println(j + " # " + j2 + " >>>>> " + rgbArr[0]
//								+ "." + rgbArr[1] + "." + rgbArr[2]);
//					}
//				}
				out.close();
				
				
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}

	/**
	 * 取得图像上指定位置像素的 rgb 颜色分量。
	 * 
	 * @param image
	 *            源图像。
	 * @param x
	 *            图像上指定像素位置的 x 坐标。
	 * @param y
	 *            图像上指定像素位置的 y 坐标。
	 * @return 返回包含 rgb 颜色分量值的数组。元素 index 由小到大分别对应 r，g，b。
	 */
	public static int[] getRGB(BufferedImage image, int x, int y) {
		int[] rgb = new int[3];
		int pixel = image.getRGB(x, y);
		rgb[0] = (pixel & 0xff0000) >> 16;
		rgb[1] = (pixel & 0xff00) >> 8;
		rgb[2] = (pixel & 0xff);

		return rgb;
	}
}

/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/
